from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect

class web(MiddlewareMixin):
    def process_request(self, request):
        # 这是前端页面,如果用户访问了这个页面都要经过这个中间件
        # 获取当前访问的路径
        path = request.path
        if path.startswith('/web/') or len(path) == 1:
            # 判断当前是否是登陆页
            if path.startswith('/web/login_and_register/'):
                return None
            else:
                # 判断是否已经登录
                user_session = request.session.get('user')
                
                if user_session:
                    return None
                else:
                    # 跳转到登陆页
                    return redirect('/web/login_and_register/')
        return None

           
    
      
    def process_response(self, request, response):
        return response
